#include <header.h>

int main(){

    char *port = "8080";
    char *ip = "0.0.0.0";
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in sock;
    sock.sin_family = AF_INET;
    sock.sin_port = htons(atoi(port));
    sock.sin_addr.s_addr = inet_addr(ip);
    bind(socket_fd, (struct sockaddr *)&sock, sizeof(sock));
    listen(socket_fd, 10);
    int net_fd= accept(socket_fd, NULL, NULL);

    // --------------------------------------
    int file_fd = open("1.txt", O_RDWR);

    while(1){
        char buf[2048]= {0};
        size_t len = 0;

        ssize_t  ret = recv(net_fd, &len, sizeof(size_t), MSG_WAITALL);
        if(ret == 0){
            break;
        }
        recv(net_fd, buf, len, MSG_WAITALL);

        write(file_fd, buf, len);
        if(len != 2048)
        printf("len: %ld \n", len);
    }

    // -----------------------------------------

    close(net_fd);
    close(socket_fd);
    return 0;
}

